package com.authine.cloudpivot.app.openapi.controller;

import com.authine.cloudpivot.app.openapi.config.SwaggerConfig;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;
import springfox.documentation.swagger.web.*;

import java.util.List;
import java.util.Optional;

/**
 * @author carter
 * create_date  2020/6/10 15:11
 * description     封装swaggerUI的请求接口
 */
@RestController
public class SwaggerController {

    private final SecurityConfiguration securityConfiguration;

    private final UiConfiguration uiConfiguration;

    private final SwaggerConfig swaggerConfig;

    public SwaggerController( SwaggerConfig swaggerConfig) {
        this.securityConfiguration = null;
        this.uiConfiguration =  UiConfigurationBuilder.builder().build();
        this.swaggerConfig = swaggerConfig;
    }

    @GetMapping("/swagger-resources/configuration/security")
    public Mono<ResponseEntity<SecurityConfiguration>> securityConfiguration() {

        return Mono.just(new ResponseEntity<>(
                Optional.ofNullable(securityConfiguration).orElse(SecurityConfigurationBuilder.builder().build())
                , HttpStatus.OK

        ));

    }

    @GetMapping("/swagger-resources/configuration/ui")
    public Mono<ResponseEntity<UiConfiguration>> uiConfiguration() {
        return Mono.just(new ResponseEntity<>(
                Optional.ofNullable(uiConfiguration).orElse(UiConfigurationBuilder.builder().build())
                , HttpStatus.OK)
        );
    }

    @GetMapping("/swagger-resources")
    public Mono<ResponseEntity<List<SwaggerResource>>> swaggerResources() {
        ResponseEntity<List<SwaggerResource>> responseEntity = new ResponseEntity<>(swaggerConfig.get(), HttpStatus.OK);
        return Mono.just(responseEntity);
    }

    @GetMapping("/csrf")
    public Mono<ResponseEntity<String>> csrf() {
        ResponseEntity<String> responseEntity = new ResponseEntity<>("", HttpStatus.OK);
        return Mono.just(responseEntity);
    }

    @GetMapping("/")
    public Mono<ResponseEntity<String>> index() {
        ResponseEntity<String> responseEntity = new ResponseEntity<>("", HttpStatus.OK);
        return Mono.just(responseEntity);
    }


}
